package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.UsmResourceApi;
import com.itcat.mapper.UmsResourceMapper;
import com.itcat.mysql.pojo.UmsResource;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/4/3 19:37]
 */
@DubboService
public class UsmResourceApiImpl implements UsmResourceApi {

    @Autowired
    private UmsResourceMapper mapper;

    /***
     * 分页模糊查询后台资源
     * @author Cr Mr
     * @date 2022/4/3 19:42
     */
    @Override
    public Page<UmsResource> selectPage(Long categoryId, String nameKeyword, String urlKeyword, Integer pageNum, Integer pageSize) {
        Page<UmsResource> menuPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.eq(categoryId != null, UmsResource::getCategoryId, categoryId).
                like(!StringUtils.isEmpty(nameKeyword), UmsResource::getName, nameKeyword).
                like(!StringUtils.isEmpty(urlKeyword), UmsResource::getUrl, urlKeyword);
        Page<UmsResource> selectPage = mapper.selectPage(menuPage, lqw);
        return selectPage;
    }

    /***
     * 添加后台资源
     * @author Cr Mr
     * @date 2022/4/3 19:54
     */
    @Override
    public void create(UmsResource umsResource) {
        mapper.insert(umsResource);
    }

    /***
     * 根据ID删除后台资源
     * @author Cr Mr
     * @date 2022/4/3 19:57
     */
    @Override
    public void delete(Long id) {
        mapper.deleteById(id);
    }

    /***
     * 查询所有后台资源
     * @author Cr Mr
     * @date 2022/4/3 20:02
     */
    @Override
    public List<UmsResource> listAll() {
        return mapper.selectList(new LambdaQueryWrapper<UmsResource>());
    }

    /***
     * 修改后台资源
     * @author Cr Mr
     * @date 2022/4/3 20:04
     */
    @Override
    public void update(Long id, UmsResource umsResource) {
        mapper.update(umsResource, new LambdaUpdateWrapper<UmsResource>().eq(UmsResource::getId, id));
    }

    /***
     * 根据ID获取资源详情
     * @author Cr Mr
     * @date 2022/4/3 20:04
     */
    @Override
    public UmsResource resource(Long id) {
        return mapper.selectById(id);
    }
}
